package info.openlvb.openface.client.handler;

import info.openlvb.openface.client.base.person.MemberInGroupWindow;
import info.openlvb.openface.client.service.LdapService;
import info.openlvb.openface.client.service.LdapServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;

public class DeleteUserInGroupListener extends ButtonListenerAdapter{

	DialogBox dialog;
	private String login;
	private String group;
	private int row;
	private MemberInGroupWindow memberInGroupWindow;
	private final LdapServiceAsync ldapService = GWT.create(LdapService.class);

	public DeleteUserInGroupListener(final MemberInGroupWindow memberInGroupWindow, final String group, final String login){
		this.login = login;
		this.group = group;
		this.memberInGroupWindow = memberInGroupWindow;
	}
	public void onClick(Button button, EventObject e){
		//System.out.println("click sur la ligne "+ row);
		dialog = new DialogBox();
		dialog.setTitle("Confirmation de suppression");
		dialog.setAnimationEnabled(true);
		dialog.setGlassEnabled(true);
		
		VerticalPanel content = new VerticalPanel();
		HTML lbl_message = new HTML("Voulez-vous vaiment supprimer cet utilisateur de ce groupe ?</br>");
		content.add(lbl_message);
		Button yesBtn = new Button("Valider");
		yesBtn.addListener(new ButtonListenerAdapter(){
			public void onClick(Button button, EventObject e){
				ldapService.removeUserInGroup(group, login, new AsyncCallback<Void>() {
					public void onFailure(Throwable caught) {
						MessageBox.alert("probleme", "probleme de communication avec le backend");
					}
					public void onSuccess(Void result) {
						memberInGroupWindow.getUserTable().removeRow(row);
						dialog.hide();
					}
				});
			}
		});
		
		
		
		Button noBtn = new Button("Abandonner");
		noBtn.addListener(new ButtonListenerAdapter(){
			public void onClick(Button button, EventObject e) {
				dialog.hide();
			}			
		});
		noBtn.setStyle("closeBtn");
		Panel btPanel = new Panel();
		btPanel.addButton(yesBtn);
		btPanel.addButton(noBtn);
		content.add(btPanel);
		dialog.add(content);
		dialog.center();
		dialog.show();
		
		/*MessageBox.show(new MessageBoxConfig(){ 
			{
				setTitle("Confirmation de suppression");
				setMsg("Voulez-vous vraiment supprimer cet utilisateur de ce groupe ?");
				setButtons(MessageBox.YESNO);
				setCallback(new PromptCallback() {
					public void execute(String btnID, String text) {
						if(btnID.equals("yes")){
							ldapService.removeUserInGroup(group, login, new AsyncCallback<Void>() {
								public void onFailure(Throwable caught) {
									MessageBox.alert("probleme", "probleme de communication avec le backend");
								}
								public void onSuccess(Void result) {
									MessageBox.alert("l'utilisateur a été supprimé du groupe");
									memberInGroupWindow.getUserTable().removeRow(row);
								}
							});
						}
					}
				});

			}
		});*/
	}
	public void setRow(int row){
		this.row = row;
	}
}
